package com.jcl.recruit.elasticsearch.core.builds;

import com.google.gson.Gson;
import com.jcl.recruit.elasticsearch.core.entity.ElasticsearchEntity;
import com.jcl.recruit.elasticsearch.core.utils.ElasticConfigUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Collections;

/**
 * @ClassName Test

 * @Author yyliu
 * @Date 2018/10/26 14:36
 * @Version 1.0
 **/
@Component
public class ElasticSearchBuild {

    @Autowired
    private RestClient restClient;

    public ElasticsearchEntity searchResume(String jsonParam) {

        HttpEntity httpEntity = new NStringEntity(jsonParam, ContentType.APPLICATION_JSON);
        try {
            Response response = restClient.performRequest(ElasticConfigUtils.ES_METHOD,
                    ElasticConfigUtils.SPLIT_URL + ElasticConfigUtils.index
                            + ElasticConfigUtils.ENDPOINT_SEARCH,
                    Collections.<String, String>emptyMap(), httpEntity);
            String json = EntityUtils.toString(response.getEntity());
            json = json.substring(json.indexOf(ElasticConfigUtils.ES_HITS) + 6, json.length() - 1);
            ElasticsearchEntity entity = new Gson().fromJson(json, ElasticsearchEntity.class);
            return entity;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String createSearchResume(String id, String jsonParam) throws Exception {
        String endpoint = ElasticConfigUtils.SPLIT_URL + ElasticConfigUtils.index +
                ElasticConfigUtils.SPLIT_URL + ElasticConfigUtils.type + ElasticConfigUtils.SPLIT_URL + id;
        HttpEntity entity = new NStringEntity(jsonParam, ContentType.APPLICATION_JSON);
        Response response = restClient.performRequest(ElasticConfigUtils.ES_METHOD_PUT, endpoint,
                Collections.<String, String>emptyMap(), entity);
        return EntityUtils.toString(response.getEntity());
    }

    public HttpEntity queryAll() throws Exception {
        String method = "POST";
        String endpoint = "/resume/_search";
        HttpEntity entity = new NStringEntity("{" +
                "  \"query\": {" +
                "    \"match_all\": {}" +
                "  }" +
                "}", ContentType.APPLICATION_JSON);

        Response response = restClient.performRequest(method, endpoint, Collections.<String, String>emptyMap(), entity);
        return response.getEntity();
    }

    public HttpEntity catApi() throws Exception {
        String method = "GET";
        String endpoint = "/_cat";
        Response response = restClient.performRequest(method, endpoint);

        return response.getEntity();
    }

}
